package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.CategoryMapper;
import org.linlinjava.litemall.db.models.Category;
import org.linlinjava.litemall.db.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Override
    public IPage<Category> queryL1WithoutRecommend(int offset, int limit) {
        Page<Category> page = new Page<>(offset, limit);
        QueryWrapper<Category> query = new QueryWrapper<>();
        query.eq("level", "L1");
        query.ne("name", "推荐");
        return baseMapper.selectPage(page, query);
    }

    @Override
    public IPage<Category> queryL1(int offset, int limit) {
        Page<Category> page = new Page<>(offset, limit);
        return baseMapper.selectPage(page, new LambdaQueryWrapper<Category>()
                .eq(Category::getLevel, "L1"));
    }

    @Override
    public List<Category> queryL1() {
        return baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getLevel, "L1"));
    }

    @Override
    public List<Category> queryByPid(Long pid) {
        return baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getPid, pid));
    }

    @Override
    public List<Category> queryL2ByIds(List<Long> ids) {
        return baseMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getLevel, "L2").in(Category::getId, ids));
    }

    @Override
    public IPage<Category> querySelective(String id, String name, Integer pageNo, Integer pageSize) {
        Page<Category> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Category> query = new QueryWrapper<>();
        query.eq(!StringUtils.isEmpty(id), "id", id);
        query.like(!StringUtils.isEmpty(name), "name", name);
        return baseMapper.selectPage(page, query);
    }

    public List<Category> queryChannel() {
        return this.queryL1();
    }
}
